package qeb.handheldmedical.user;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import qeb.handheldmedical.R;
import qeb.handheldmedical.adapter.PhoneAdapter;
import qeb.handheldmedical.adapter.SlideListView;
import qeb.handheldmedical.dialog.MultiDialog;
import qeb.handheldmedical.helper.WSHelper;
import qeb.handheldmedical.model.Phone;
import qeb.handheldmedical.model.User;
import qeb.handheldmedical.patient.InPatientList;

public class PhoneList extends Activity {
	private SlideListView slideListView;
	private PhoneAdapter adapter;
	User usr=new User();
	private JSONArray phoneJsonArray;
	Phone phone;
	private JSONObject jobj;
	private Button btn_title_back;
	private TextView tv_title; 
	private MultiDialog mDialog;
	
	private static final int UPDATE=0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		usr=(User)getIntent().getSerializableExtra("userinfo");
		setContentView(R.layout.query_phone);
		initView();
		mDialog = new MultiDialog(PhoneList.this,
				MultiDialog.DialogType.WAITING);
		mDialog.setMessage("正在加载，请稍候......");
		mDialog.show();
		new Thread(){
			
			@Override
			public void run() {
				try {
					getData();
			        myHandler.sendEmptyMessage(UPDATE);
				} catch (Exception e) {
					// TODO: handle exception
					Log.v("PhoneList", e.toString());
				}
			}
		}.start(); 
	}
	
	private void initView()
	{
		slideListView = (SlideListView) findViewById(R.id.slideListView9);
		tv_title=(TextView)findViewById(R.id.tv_top_title);
		tv_title.setText("设备列表");
		btn_title_back=(Button)findViewById(R.id.btn_title_back);
		btn_title_back.setOnClickListener(MyOnClickListener);
		//getData();

	}
	
	private void getData()
	{
		phoneJsonArray = WSHelper.GetPhoneList();		
	}
	
	/*系统主菜单点击事件（在院病人、门诊病人等）*/
	private OnClickListener MyOnClickListener=new OnClickListener(){		
		@Override
		public void onClick(View v) {
			// TODO 自动生成的方法存根LisAdapter
			//Bundle bundle=new Bundle();
			switch (v.getId()) {
			case R.id.btn_title_back://返回按钮
				PhoneList.this.finish();
				break;
			default:
				break;		
		}
		}
	};
	private void dataBind()
	{
		if (phoneJsonArray != null && phoneJsonArray.length() > 0) {
			adapter = new PhoneAdapter(phoneJsonArray.length(),
					PhoneList.this, phoneJsonArray);
			
			slideListView.setAdapter(adapter);
			
			slideListView.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					phone = new Phone();
					try {
						jobj = phoneJsonArray.getJSONObject(position);
						phone.getValue(jobj);
						if(phone.Applystate.equals("1")){
							mDialog = new MultiDialog(PhoneList.this,
									MultiDialog.DialogType.CONFIRM);
							mDialog.setGoButtonText("作废");
							mDialog.setTitle("设备验证");
							mDialog.setMessage(phone.Deviceid+","+phone.Applicantname+","+phone.Applicantid+"\n目前状态:已通过审核!");
							mDialog.setDialogGoListener(new OnClickListener(){

								Toast tt;
								@Override
								public void onClick(View arg0) {
									// TODO Auto-generated method stub
									phone.Applystate="-1";
									phone.Cancelid=usr.GH;
									phone.Cancelname=usr.DNAME;
									phone=WSHelper.UpdatePhoneInfo(phone.toJSONObject().toString());
									
									if(phone.Deviceid!=null){
										tt = Toast.makeText(PhoneList.this,
												"作废成功！", Toast.LENGTH_LONG);
										tt.show();
										mDialog.cancel();
										new Thread(){
											
											@Override
											public void run() {
												try {
													getData();
											        myHandler.sendEmptyMessage(UPDATE);
												} catch (Exception e) {
													// TODO: handle exception
													Log.v("PhoneList", e.toString());
												}
											}
										}.start(); 
									}
									else{
										tt = Toast.makeText(PhoneList.this,
												"作废操作失败，请重试！", Toast.LENGTH_LONG);
										tt.show();
									}
										
								}
								
							});
							mDialog.show();
						}
						else{
							mDialog = new MultiDialog(PhoneList.this,
									MultiDialog.DialogType.CONFIRM);
							mDialog.setGoButtonText("通过");
							mDialog.setTitle("设备验证");
							if(phone.Applystate.equals("-1")){
								mDialog.setMessage(phone.Deviceid+","+phone.Applicantname+","+phone.Applicantid+"\n目前状态:已作废!");
							}
							else{
								mDialog.setMessage(phone.Deviceid+","+phone.Applicantname+","+phone.Applicantid+"\n目前状态:未审核!");
							}
							mDialog.setDialogGoListener(new OnClickListener(){
								Toast tt;
								@Override
								public void onClick(View arg0) {
									// TODO Auto-generated method stub
									phone.Applystate="1";
									phone.Checkerid=usr.GH;
									phone.Checkername=usr.DNAME;
									phone=WSHelper.UpdatePhoneInfo(phone.toJSONObject().toString());
									
									if(phone.Deviceid!=null){
										tt = Toast.makeText(PhoneList.this,
												"审核成功！", Toast.LENGTH_LONG);
										tt.show();
										mDialog.cancel();
										new Thread(){
											
											@Override
											public void run() {
												try {
													getData();
											        myHandler.sendEmptyMessage(UPDATE);
												} catch (Exception e) {
													// TODO: handle exception
													Log.v("PhoneList", e.toString());
												}
											}
										}.start(); 
									}
									else{
										tt = Toast.makeText(PhoneList.this,
												"审核操作失败，请重试！", Toast.LENGTH_LONG);
										tt.show();
									}
								}
								
							});
							mDialog.show();
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						// e.printStackTrace();
					} catch (InstantiationException e) {
						// TODO Auto-generated catch block
						// e.printStackTrace();
					}
				}

			});
		}
	}
	
	Handler myHandler=new Handler(){
		@Override
		public void handleMessage(Message msg){
			switch(msg.what){
			case UPDATE:
				dataBind();
				//btn_search.setClickable(true);
				break;
			default:
				break;
			}
			super.handleMessage(msg);
			//isRunning=false;
			mDialog.cancel();
		}
	};
}
